﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using RapChieuPhim.DAO;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class PhimDAO
    {
        public PhimDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaPhim", id);
            DataTable dt = cn.SqlExcuteQuery("LayPhimTheoID", param);
            PhimDTO Phim = new PhimDTO();

            if (dt.Rows.Count != 0)
            {
                Phim.MaPhim = int.Parse(dt.Rows[0]["MaPhim"].ToString());
                Phim.TenPhim = dt.Rows[0]["TenPhim"].ToString();
                Phim.NamSanXuat = int.Parse(dt.Rows[0]["NamSanXuat"].ToString());
                Phim.NuocSanXuat = dt.Rows[0]["NuocSanXuat"].ToString();
                Phim.HangPhim = dt.Rows[0]["HangPhim"].ToString();
                Phim.GioiThieu = dt.Rows[0]["GioiThieu"].ToString();
                Phim.Trailer = dt.Rows[0]["Trailer"].ToString();
                Phim.Poster = dt.Rows[0]["Poster"].ToString();
                Phim.ThoiLuongPhim = int.Parse(dt.Rows[0]["ThoiLuongPhim"].ToString());
                Phim.DinhDang3D = Boolean.Parse(dt.Rows[0]["_3D"].ToString());
                Phim.DinhDang2D= Boolean.Parse(dt.Rows[0]["_2D"].ToString());
                Phim.DsDienVien = dt.Rows[0]["DSDienVien"].ToString();
                Phim.TheLoai = TheLoaiBUS.GetByID(int.Parse(dt.Rows[0]["MaTL"].ToString()));
            }
            return Phim;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllPhim");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    PhimDTO Phim = new PhimDTO();
                    Phim.MaPhim = int.Parse(dr["MaPhim"].ToString());
                    Phim.TenPhim = dr["TenPhim"].ToString();
                    Phim.NamSanXuat = int.Parse(dr["NamSanXuat"].ToString());
                    Phim.NuocSanXuat = dr["NuocSanXuat"].ToString();
                    Phim.HangPhim = dr["HangPhim"].ToString();
                    Phim.GioiThieu= dr["GioiThieu"].ToString();
                    Phim.Trailer = dr["Trailer"].ToString();
                    Phim.Poster = dr["Poster"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(dr["ThoiLuongPhim"].ToString());
                    Phim.DinhDang3D = Boolean.Parse(dr["_3D"].ToString());
                    Phim.DinhDang2D = Boolean.Parse(dr["_2D"].ToString());
                    Phim.DsDienVien = dr["DSDienVien"].ToString();
                    Phim.TheLoai = TheLoaiBUS.GetByID(int.Parse(dr["MaTL"].ToString()));
                    array.Add(Phim);
                }
            }
            return array;
        }

        public void Insert(PhimDTO Phim)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[12];
            param[0] = new SqlParameter("@TenPhim", Phim.TenPhim);
            param[1] = new SqlParameter("@NamSanXuat", Phim.NamSanXuat);
            param[2] = new SqlParameter("@NuocSanXuat", Phim.NuocSanXuat);
            param[3] = new SqlParameter("@HangPhim", Phim.HangPhim);
            param[4] = new SqlParameter("@GioiThieu", Phim.GioiThieu);
            param[5] = new SqlParameter("@Trailer", Phim.Trailer);
            param[6] = new SqlParameter("@Poster", Phim.Poster);
            param[7] = new SqlParameter("@ThoiLuongPhim", Phim.ThoiLuongPhim);
            param[8] = new SqlParameter("@3D", Phim.DinhDang3D);
            param[9] = new SqlParameter("@2D", Phim.DinhDang2D);
            param[10] = new SqlParameter("@DSDienVien", Phim.DsDienVien);
            param[11] = new SqlParameter("@MaTL", Phim.TheLoai);
            cn.SqlExcuteNonQuery("InsertPhim", param);
        }

        public void Update(PhimDTO Phim)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[13];
            param[0] = new SqlParameter("@MaPhim", Phim.MaPhim);
            param[1] = new SqlParameter("@TenPhim", Phim.TenPhim);
            param[2] = new SqlParameter("@NamSanXuat", Phim.NamSanXuat);
            param[3] = new SqlParameter("@NuocSanXuat", Phim.NuocSanXuat);
            param[4] = new SqlParameter("@HangPhim", Phim.HangPhim);
            param[5] = new SqlParameter("@GioiThieu", Phim.GioiThieu);
            param[6] = new SqlParameter("@Trailer", Phim.Trailer);
            param[7] = new SqlParameter("@Poster", Phim.Poster);
            param[8] = new SqlParameter("@ThoiLuongPhim", Phim.ThoiLuongPhim);
            param[9] = new SqlParameter("@3D", Phim.DinhDang3D);
            param[10] = new SqlParameter("@2D", Phim.DinhDang2D);
            param[11] = new SqlParameter("@DSDienVien", Phim.DsDienVien);
            param[12] = new SqlParameter("@MaTL", Phim.TheLoai);
            cn.SqlExcuteNonQuery("UpdatePhim", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaPhim", id);
            cn.SqlExcuteNonQuery("DeletePhim", param);
        }
    }
}
